package com.bjfu.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjfu.common_utils.R;
import com.bjfu.eduservice.entity.EduCourse;
import com.bjfu.eduservice.entity.vo.CourseInfoVo;
import com.bjfu.eduservice.entity.vo.CoursePublishVo;
import com.bjfu.eduservice.entity.vo.CourseQuery;
import com.bjfu.eduservice.service.EduChapterService;
import com.bjfu.eduservice.service.EduCourseDescriptionService;
import com.bjfu.eduservice.service.EduCourseService;
import com.bjfu.eduservice.service.EduVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Wrapper;
import java.util.*;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Guli
 * @since 2020-07-04
 */
@Api(tags = "课程管理")
@RestController
@RequestMapping("/eduservice/course")
//@CrossOrigin
public class EduCourseController {
    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private EduVideoService videoService;

    @Autowired
    private EduCourseDescriptionService descriptionService;


    @ApiOperation("课程列表")
    @PostMapping("getCourseListByPage/{current}/{limit}")
    public R getCourseListByPage(
            @ApiParam(value = "当前页",required = true)
            @PathVariable long current,
            @ApiParam(value = "每页记录数",required = true)
            @PathVariable long limit,
            @ApiParam(value = "查询条件<br>课程名<br>课程状态")
            @RequestBody CourseQuery courseQuery
            ){
        Page<EduCourse> page = new Page<>(current,limit);
        courseService.getCourseListByPage(page,courseQuery);
        long total = page.getTotal();
        List<EduCourse> rows = page.getRecords();
        return R.ok().data("total",total).data("rows",rows);
    }

    @ApiOperation("添加课程简介")
    @PostMapping("/addcourse")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfo){
        String id = courseService.saveCourse(courseInfo);
        return R.ok().data("courseId",id);
    }


    @ApiOperation("获取课程简介")
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(
            @ApiParam(name = "courseId",value = "课程Id",required = true)
            @PathVariable String courseId
    ){
        CourseInfoVo courseInfo = courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfo",courseInfo);
    }

    @ApiOperation("更新课程简介")
    @PostMapping("/updateCourseInfo")
    public R updateCourseInfo(
            @RequestBody CourseInfoVo courseInfoVo){
        courseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    @ApiOperation("获取课程发布信息")
    @GetMapping("getPublishCourseInfo/{courseId}")
    public R getPublishCourseInfo(
            @ApiParam(name = "courseId",value = "课程Id",required = true)
            @PathVariable String courseId
    ){
        CoursePublishVo publishVo = courseService.getPublishCourseInfo(courseId);
        return R.ok().data("publishInfo",publishVo);
    }

    @ApiOperation("课程最终发布")
    @PostMapping("publishCourse/{id}")
    public R publishCourse(
            @ApiParam(name = "id",value = "课程Id",required = true)
            @PathVariable() String id
    ){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        boolean success = courseService.updateById(eduCourse);
        if(success){
            return R.ok();
        }else {
            return R.error().message("课程发布失败");
        }
    }

    @ApiOperation("删除课程")
    @DeleteMapping("deleteCourse/{id}")
    public R deleteCourse(
            @ApiParam(name = "id",value = "课程Id",required = true)
            @PathVariable() String id
    ){
        // 1. 根据课程id删除课程小节
        videoService.removeByCourseId(id);

        // 2.根据课程id删除课程章节
        chapterService.removeByCourseId(id);

        // 3.根据课程id删除课程描述
        descriptionService.removeById(id);

        // 4.根据课程id删除课程信息
        courseService.deleteCourseById(id);
        return R.ok();
    }
}

